package ThreadDemo1;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-15
 * Time: 21:46
 */
public class ThreadDemo27 {
    public static void main(String[] args) throws InterruptedException {
        // java中的原子类，基于CAS实现，使用时不需要加锁
        AtomicInteger count = new AtomicInteger();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                count.getAndIncrement();//count++
                count.incrementAndGet();//++count 反之 decrement
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                count.getAndIncrement();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count " + count.get());
    }
}
